var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    uid: '',
    token: '',
    username:'',
    openid: '',
    minHeight: 0,
    minWidth: 0,
    jin_e:0,
    colorSet: '',

    catatype: '',//认证类型 10=>'咨客', 20=>'经销商', 30=>'供货商'

    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tc = app.get_tc();
    var coin = options.coin ? options.coin : 100;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          minHeight: res.windowHeight,
          minWidth: res.windowWidth,
          jin_e: coin,
          catatype: options.authtype,
          tc: tc,
        });
      },
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
    });

    app.LoginUserInfo(function (res) {
      that.setData({
        uid: res.uid,
        token: res.token,
        openid: res.wxsmall_openid,
      });
    });

    app.GetUserInfo(that.data.uid, function (res) {
      var userName = res.uname;
      if (!userName) {
        userName = res.realname;
      }
      that.setData({
        username: userName
      });
    });

  },

  formSubmit: function(e) {
    console.log("formId ", e.detail.formId);
    app.saveFormId(e.detail.formId);

    var totalFee = e.detail.value.jin_e;
    // if (totalFee < 1) {
    //   wx.showToast({
    //     title: '请输入大于1元',
    //   })
    //   return;
    // }

    var that = this;
    var uname = that.data.username;
    var optionTime = Date.now / 1000;
    var openid = that.data.openid;
    var uid = that.data.uid;
    var token = that.data.token;
    console.log("uid = " + uid + "; token = " + token);
    var privatekey = utilMd5.hexMD5(uid + "wxsmallpaysign" + token);

    var data = {
      "publickey": util.publiccode,
      "ac": "wxsmallpaysign",
      "subject": uname + ' 代理商代理费用',
      "uid": uid,
      "openid": openid,
      "privatekey": privatekey,
      "total_fee": totalFee,
      "catatype": that.data.catatype
      // "total_fee": '1.01'
    };
    
    console.log("chongzhi data:", data);
    
    //下单请求
    util.api({
      data: data,
      method: 'POST',
      success: function (responseData) {
        
        var data = responseData.data;
        console.log("充值下单成功：", data);  
        var order_no = data.out_trade_no;
        var total_fee = data.total_fee;
        if (data.res) {
          //挂起微信支付请求
          var orderData = data.data;
          var package_ = orderData.package;
          console.log(package_);
          wx.requestPayment(
            {
              'timeStamp': orderData.timeStamp + "",
              'nonceStr': orderData.nonceStr,
              'package': package_,
              'signType': 'MD5',
              'paySign': orderData.paySign,
              'success': function (res) {
                console.log("充值支付成功：", res);
                app.dialog('充值成功', 'success', function () {
                  var pagelist = getCurrentPages();
                  if (pagelist.length > 1) {
                    //获取上一个页面实例对象
                    var prePage = pagelist[pagelist.length - 2];
                    if (prePage.set_YaJinInfo){
                      prePage.set_YaJinInfo(order_no, total_fee);
                    }
                  }
                  wx.navigateBack({
                    delta: 1
                  })
                });
              },
              'fail': function (res) {
                console.log(res);
                if (res.errMsg == 'requestPayment:fail cancel') {

                } else {
                  app.dialog('充值失败', 'loading');
                }
              }
            });
        } else {
          app.dialog('充值失败' + data.msg, 'loading');
        }
      }
    });
  },


})